package qdu.edu.cn.storm.client;

import org.apache.storm.Config;
import org.apache.storm.security.auth.SimpleTransportPlugin;
import org.apache.storm.security.auth.plain.PlainSaslTransportPlugin;
import org.apache.storm.thrift.TException;
import org.apache.storm.thrift.transport.TTransportException;
import org.apache.storm.utils.DRPCClient;
//import org.apache.storm.utils.Utils;


//import java.util.Map;

import static qdu.edu.cn.storm.ConstantFinalString.*;

public class DRPCSearchWebClient {
    private final static Config config = new Config();
    private static DRPCClient drpcClient;
    static {
        // 暂时不知道原因，只知道需要手动配置下面的参数storm.thrift.transport，即使配置文件中已经有该配置，否则报错：Frame size (67108864) larger than max length (1048576)!
//        Map<String, Object> defaultConfig = Utils.readDefaultConfig();
//        config.putAll(defaultConfig);
        config.put(Config.DRPC_THRIFT_TRANSPORT_PLUGIN, PlainSaslTransportPlugin.class.getName());
//        config.put(Config.DRPC_THRIFT_TRANSPORT_PLUGIN, SimpleTransportPlugin.class.getName());
        config.put(Config.STORM_NIMBUS_RETRY_TIMES, 3);
        config.put(Config.STORM_NIMBUS_RETRY_INTERVAL, 10);
        config.put(Config.STORM_NIMBUS_RETRY_INTERVAL_CEILING, 20);
//        config.put(Config.DRPC_MAX_BUFFER_SIZE, 104857600);
        try {
            drpcClient = new DRPCClient(config, HOST, STORM_DRPC_PORT);
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

    public DRPCSearchWebClient() {
    }

    public static String execute(String args) {
        String result = null;

        try {
            result = drpcClient.execute(FUNCTION, args);
        } catch (TException e) {
            e.printStackTrace();
        }

        return result;
    }
}
